package cn.com.allunion.sms.service.impl;

import cn.com.allunion.sms.service.SMSBusinessService;
import com.cn.dao.util.HibernateDAO;
import com.cn.dao.util.Page;
import com.cn.entity.SMSBusiness;
import com.cn.entity.UserBase;
import com.cn.entity.UserBase_SMSBusiness;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 业务逻辑接口实现类
 * Created by YangJie on 2016/4/13.
 */
@Service
public class SMSBusinessServiceImpl implements SMSBusinessService {
    /**
     * 页面大小
     */
    @Value("${pagesize}")
    int pagesize;
    /**
     * 打印日志
     */
    public Boolean isCached = true;

    @Autowired
    HibernateDAO hibernateDao;

    /**
     * 添加一个新业务
     *
     * @param smsBusinessName
     *            业务名称
     * @param smsBusinessDesc
     *            业务描述
     * @param businessCode
     *            业务代码
     * @param businessState
     *            业务状态
     * @param businessJoinNum
     *            业务接入号
     * @param businessSignature
     *            业务签名
     * @return 方法执行结果
     */

    @Transactional(rollbackForClassName = "Exception")
    public String addNewBusiness(String smsBusinessName,
                                 String smsBusinessDesc, String businessCode, String businessState,
                                 String businessJoinNum, String businessSignature) {
        SMSBusiness smsBusiness = new SMSBusiness();
        smsBusiness.setSmsBusinessName(smsBusinessName);
        smsBusiness.setSmsBusinessDesc(smsBusinessDesc);
        smsBusiness.setBusinessCode(businessCode);
        smsBusiness.setBusinessState(businessState);
        smsBusiness.setBusinessJoinNum(businessJoinNum);
        smsBusiness.setBusinessSignature(businessSignature);
        return this.hibernateDao.saveOrUpdate(smsBusiness);
    }

    /**
     * 存储指定业务
     *
     * @param sms
     *            短信业务实例
     */
    @Transactional(rollbackForClassName = "Exception")
    public void saveOrUpdate(SMSBusiness sms) {
        hibernateDao.saveOrUpdate(sms);
    }

    /**
     * 修改业务
     *
     * @param id
     *            业务ID
     * @param smsBusinessName
     *            业务名称
     * @param smsBusinessDesc
     *            业务描述
     * @param businessCode
     *            业务代码
     * @param businessState
     *            业务状态
     * @param businessJoinNum
     *            业务接入号
     * @param businessSignature
     *            业务签名
     * @return 方法执行结果
     */
    @Transactional(rollbackForClassName = "Exception")
    public String updateBusiness(String id, String smsBusinessName,
                                 String smsBusinessDesc, String businessCode, String businessState,
                                 String businessJoinNum, String businessSignature) {
        SMSBusiness smsb = hibernateDao.listById(SMSBusiness.class, id, true);
        smsb.setSmsBusinessName(smsBusinessName);
        smsb.setSmsBusinessDesc(smsBusinessDesc);
        smsb.setBusinessCode(businessCode);
        smsb.setBusinessState(businessState);
        smsb.setBusinessJoinNum(businessJoinNum);
        smsb.setBusinessSignature(businessSignature);
        smsb.setUpdateDate(new Date());
        return hibernateDao.saveOrUpdate(smsb);
    }

    /**
     * 根据id查找业务信息
     *
     * @param id
     * @return 短信业务
     */
    @Transactional(readOnly = true)
    public SMSBusiness findbyId(String id) {
        return hibernateDao.listById(SMSBusiness.class, id, false);
    }

    /**
     * 查询所有的业务列表
     *
     * @return 业务列表
     */
    @Transactional(readOnly = true)
    public List<SMSBusiness> findallSMSBusiness() {
        return hibernateDao.listByAll(SMSBusiness.class, isCached);
    }

    /**
     * 查询所有的业务列表--page
     *
     * @param pageCurrent
     *            当前页号
     * @return 所有的业务列表分页显示
     */
    @Transactional(readOnly = true)
    public Page<SMSBusiness> findallSMSBusiness(int pageCurrent) {

        int allcount = hibernateDao.count(SMSBusiness.class, false);

        int lastPage = lastPage(allcount, pagesize);
        if (lastPage < pageCurrent) {
            pageCurrent = lastPage;
        }
        if (lastPage <= 1) {
            pageCurrent = 0;
        }
        List<SMSBusiness> list = hibernateDao.listByPage(SMSBusiness.class,
                pagesize, pageCurrent, isCached);

        Page<SMSBusiness> page = new Page<SMSBusiness>();
        page.setList(list);
        page.setTotal(allcount);
        page.setPageSize(pagesize);
        page.setPageTotal(lastPage);
        return page;
    }

    /**
     * 相对位置
     *
     * @param count
     *            页数
     * @param pageSize
     *            分页长度
     * @return 相对位置
     */
    public int lastPage(int count, int pageSize) {
        if (count % pageSize == 0) {
            return count / pageSize;
        }

        return count / pageSize + 1;
    }

    public int getPagesize() {
        return pagesize;
    }

    public void setPagesize(int pagesize) {
        this.pagesize = pagesize;
    }

    /**
     * 查询某个管理员未配置的业务列表
     *
     * @param id
     *            用户ID
     * @return 业务列表
     */
    @Transactional(readOnly = true)
    public List<SMSBusiness> findallSMSBusiness(String id) {
        List<SMSBusiness> list = new ArrayList<SMSBusiness>();
        UserBase ub = hibernateDao.listById(UserBase.class, id, isCached);
        List<SMSBusiness> listall = hibernateDao.listByAll(SMSBusiness.class,
                isCached);
        List<UserBase_SMSBusiness> ubsm = hibernateDao.listByCriteria(
                UserBase_SMSBusiness.class, Restrictions.eq("userBase", ub),
                isCached);
        if ((listall != null && listall.size() != 0)
                && (ubsm != null && ubsm.size() != 0)) {
            for (SMSBusiness sb : listall) {
                Boolean b = true;
                for (UserBase_SMSBusiness ubs : ubsm) {
                    if (ubs.getSmsBusiness().getSmsBusinessID()
                            .equals(sb.getSmsBusinessID())) {
                        b = false;
                    }
                }
                if (b) {

                    list.add(sb);
                }
            }

        } else {
            list = listall;
        }
        return list;

    }

    /**
     * 查询一个管理员的配置列表
     *
     * @param id 用户ID
     * @return 业务列表
     */
    @Transactional(readOnly = true)
    public List<SMSBusiness> findSMSBusinessbyus(String id) {
        List<SMSBusiness> list = new ArrayList<SMSBusiness>();
        UserBase ub = hibernateDao.listById(UserBase.class, id, isCached);
        List<UserBase_SMSBusiness> ubsm = hibernateDao.listByCriteria(
                UserBase_SMSBusiness.class, Restrictions.eq("userBase", ub),
                isCached);
        // List<UserBase_Authority> ubsm = hibernateDao.listByCriteria(
        // UserBase_Authority.class, Restrictions.eq("userBase", ub),
        // isCached);
        if (ubsm != null && ubsm.size() != 0) {
            for (UserBase_SMSBusiness ubs : ubsm) {
                list.add(ubs.getSmsBusiness());
            }
        }
        return list;
    }

    /**
     * 根据id查找返回状态
     *
     * @param id
     * @return 返回状态
     */
    @Transactional(readOnly = true)
    public int findState(String id) {
        String result = "";
        List<SMSBusiness> smsbList = hibernateDao.listByCriteria(
                SMSBusiness.class, Restrictions.eq("smsBusinessID", id), true);
        if (smsbList.size() != 0) {
            result = smsbList.get(0).getBusinessState();
            return Integer.valueOf(result);
        }
        return -2;
    }

    /**
     * 通过业务代码查找业务
     *
     * @param code
     *            业务代码
     * @return 业务
     */
    @Transactional(readOnly = true)
    public List<SMSBusiness> findbyCode(String code) {
        List<SMSBusiness> sList = hibernateDao.listByCriteria(
                SMSBusiness.class, Restrictions.eq("businessCode", code), true);

        return sList;
    }

    /**
     * 根据businessCode 查找 ，返回对象
     *
     * @param businessCode
     *            业务代码
     * @return 业务
     */
    @Transactional(readOnly = true)
    public SMSBusiness findBussinessObj(String businessCode) {
        List<SMSBusiness> smsbList = hibernateDao.listByCriteria(
                SMSBusiness.class,
                Restrictions.eq("businessCode", businessCode), true);
        if (smsbList.size() != 0) {
            return smsbList.get(0);
        } else {
            return null;
        }
    }

    /**
     * 测试方法
     *
     * @return 测试结果
     */
    public String findBussiness() {
        return "000";
    }
}
